package com.iwhalecloud.bss.kite.cucc.order.save.impl;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.order.save.ISaveHandler;
import com.iwhalecloud.bss.kite.cucc.order.util.TableUtils;
import com.iwhalecloud.bss.kite.dataservice.enums.TableConfig;
import com.ztesoft.bss.base.extmapper.BaseMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 数据入库处理器
 * @param <T>
 */
@Component
public class BaseSaveHandler<T> implements ISaveHandler<T> {

    @Override
    public void insert(List<T> entitys, String tableName) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }

        BaseMapper<T> mapper = TableUtils.getMapper(tableName, entitys.get(0).getClass());
        mapper.insertBatch(entitys);
    }

    @Override
    public void update(List<T> entitys, String tableName) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }

        BaseMapper<T> mapper = TableUtils.getMapper(tableName, entitys.get(0).getClass());
        for (T entity : entitys) {
            mapper.updateByPrimaryKeySupportSliceKey(entity);
        }
    }

    @Override
    public void updateSelective(List<T> entitys, String tableName) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }

        BaseMapper<T> mapper = TableUtils.getMapper(tableName, entitys.get(0).getClass());
        for (T entity : entitys) {
            mapper.updateByPrimaryKeySupportSliceKeySelective(entity);
        }
    }

    @Override
    public void delete(List<T> entitys, String tableName) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }

        BaseMapper<T> mapper = TableUtils.getMapper(tableName, entitys.get(0).getClass());
        for (T entity : entitys) {
            mapper.deleteByPrimaryKeySupportSliceKey(entity);
        }
    }

    @Override
    public void transfer(List<T> entitys, String tableName) {
        if (CollectionUtils.isEmpty(entitys)) {
            return;
        }

        TableConfig tableConfig = EnumUtils.getEnum(TableConfig.class, StringUtils.upperCase(tableName));
        if (Objects.isNull(tableConfig)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("no table config [TableConfig." + tableName + "]");
        }

        String sliceKeyField = tableConfig.getSliceKeyField();
        String newSliceKeyField = "new" + StringUtils.capitalize(sliceKeyField);
        Class<?> clazz = entitys.get(0).getClass();
        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(clazz, newSliceKeyField);
        if (Objects.isNull(propertyDescriptor)) {
            ErrorConsts.BUSI_COMMON_ERROR
                .throwOut("no such field [" + newSliceKeyField + "] in [" + clazz.getName() + "]");
        }

        List<T> insertEntitys = new ArrayList<>();
        BaseMapper<T> mapper = TableUtils.getMapper(tableName, entitys.get(0).getClass());
        for (T entity : entitys) {
            String custId = KiteBeanUtils.getValueByName(entity, sliceKeyField);
            String newCustId = KiteBeanUtils.getValueByName(entity, newSliceKeyField);
            if (StringUtils.isBlank(newCustId) || KiteStringUtils.equals(custId, newCustId)) {
                continue;
            }

            mapper.deleteByPrimaryKeySupportSliceKey(entity);
            KiteBeanUtils.setValueByName(entity, sliceKeyField, newCustId);
            insertEntitys.add(entity);
        }

        mapper.insertBatch(insertEntitys);
    }
}
